package com.army.stock.web;

import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmyCategory;
import com.army.stock.domain.ArmySupplier;
import com.army.stock.service.ArmyCategoryService;
import com.army.stock.util.CommonRegExp;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangyong on 14/12/29.
 * 货品类别
 */
@Controller
public class ArmyCategoryController {

    private final static Logger logger = LoggerFactory.getLogger(ArmyCategoryController.class);

    @Autowired
    private ArmyCategoryService armyCategoryService;

    private final static String CATEGORIES_PAGE = "category/categories";

    private final static String NEW_PAGE = "category/new";

    private final static String UPDATE_PAGE = "category/edit";

    @RequestMapping(value = "/categories", method = RequestMethod.GET)
    public String categories(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        PaginatedArrayList<ArmyCategory> pageInfo = armyCategoryService.getArmyCategoryPageInfo(baseUrl.getCurrentPage(), 10, baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return CATEGORIES_PAGE;
    }

    @RequestMapping(value = "/categories/new", method = RequestMethod.GET)
    public String addCategoryPage(HttpServletRequest request, ModelMap modelMap) {
        return NEW_PAGE;
    }

    @RequestMapping(value = "/categories/add", method = RequestMethod.POST)
    public String add(HttpServletRequest request, ModelMap modelMap, ArmyCategory armyCategory) {
        if (StringUtils.isBlank(armyCategory.getCategoryCode())) {
            modelMap.put("errorMsg", "类别编码不能为空");
            return NEW_PAGE;
        }
        if (!armyCategory.getCategoryCode().matches(CommonRegExp.CHARACTER_NUMBER)) {
            modelMap.put("errorMsg", "类别编码只能为英文中文");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armyCategory.getCategoryName())) {
            modelMap.put("errorMsg", "类别名称不能为空");
            return NEW_PAGE;
        }
        armyCategoryService.addArmyCategory(armyCategory);
        return "redirect:/categories";
    }

    @RequestMapping(value = "/categories/{categoryId}/update", method = RequestMethod.GET)
    public String editPage(@PathVariable(value = "categoryId") String categoryId, HttpServletRequest request, ModelMap modelMap) {
        logger.info("转向修改类型页面categoryId:" + categoryId);
        modelMap.put("categoryId", categoryId);
        ArmyCategory armyCategory = armyCategoryService.getArmyCategoryById((Long.valueOf(categoryId)));
        modelMap.put("armyCategory", armyCategory);
        return UPDATE_PAGE;
    }

    @RequestMapping(value = "/categories/update", method = RequestMethod.POST)
    public String update(HttpServletRequest request, ModelMap modelMap, ArmyCategory armyCategory) {
        if (StringUtils.isBlank(armyCategory.getCategoryCode())) {
            modelMap.put("errorMsg", "类型编码不能为空");
            return NEW_PAGE;
        }
        if (StringUtils.isBlank(armyCategory.getCategoryName())) {
            modelMap.put("errorMsg", "类型名称不能为空");
            return NEW_PAGE;
        }
        armyCategoryService.updateArmyCategory(armyCategory);
        return "redirect:/categories";
    }

    @RequestMapping(value = "/categories/del", method = RequestMethod.POST)
    @ResponseBody
    public String del(HttpServletRequest request, ModelMap modelMap) {
        String id = request.getParameter("id");
        Integer affectedRows = armyCategoryService.delArmyCategory(Long.valueOf(id));
        return String.valueOf(affectedRows);
    }

}
